import { WeaForm, WeaTableNew } from 'comsMobx';
import { WeaLocaleProvider } from 'ecCom';
import { observable, action, runInAction } from 'mobx';
import { Base } from '../base';
import * as API from '../../apis/withdraw/withDrawApi';

const { TableStore } = WeaTableNew;
const getLabel = WeaLocaleProvider.getLabel;

export default class WithDrawStore extends Base {
  form = new WeaForm();
  condition = [];
  refid = 'width';
	/**
	 * 管理的表格store
	 * @type {{}}
	 */
  @observable tableStore = {}
	/**
	 * 需要提交的参数
	 * @type {{doneSk: Array, todoSk: Array, myTodo: string}}
	 */
  @observable submitparams = {
    doneSk: [],
    todoSk: [],
    myTodo: '',
  };
	/**
	 * 是否加载中
	 * @type {boolean}
	 */
  @observable load = false;
  @observable loadSignInput = false;
	/**
	 * 核心参数从后台获取
	 * @type {{isManager: boolean, showSignInput: boolean, signInputRequired: boolean, showMyTodo: boolean, showDone: boolean, showTodo: boolean, title: string}}
	 */
  coreparams = {
    isManager: true,
    showSignInput: true,
    signInputRequired: true,
    showMyTodo: true,
    showDone: true,
    showTodo: true,
    title: getLabel(32025, '撤回'),
  }
  workparams = {};

	/**
	 * 初始化
	 * @param params
	 * @returns {Promise<void>}
	 */
  @action
  initLoad = async (params = {}) => {
    this.workparams = { ...params, ...this.form.getFormParams() };
    this.reloadLoad();
    this.getSignInput();
  }

  reloadLoad = (reload = false) => {
    let params = { ...this.workparams, ...this.coreparams, ...this.form.getFormParams() };
    API.loadWithDrawData(params).then((res) => {
      const { api_status = false, status = false, msg = '' } = res;
      if (api_status || status) {
        this.doInit(res, reload);
      } else {
        this.showErrorMsg(msg || getLabel(126400, '获取数据失败稍后重试！'));
      }
    });
  }

  getSignInput = () => {
    this.loadSignInput = false;
    API.getSignInputData(this.workparams).then((datas) => {
      runInAction(() => {
        this.loadSignInput = true;
        this.signinput = { ...datas };
      });
    });
  }

	/**
	 * 处理初始化的动作
	 * @param params
	 */
  doInit = (params, reload = false) => {
    const {
      isManager = true, showSignInput = true, signInputRequired = true,
      showMyTodo = true, showDone = true, showTodo = true, title = getLabel(32025, '撤回'),
      donesessionkey = '',
      requestid, conditions,
    } = params;
    let doenTableStore = null;
    if (showDone) {
      doenTableStore = (new TableStore()).getDatas(donesessionkey);
    }
    this.coreparams = {
      isManager,
      showSignInput,
      signInputRequired,
      showMyTodo,
      showDone,
      showTodo,
      title,
      requestid,
    };
    this.tableStore = { doenTableStore };
    this.load = true;
    if (!reload) {
      this.condition = conditions;
      this.form.initFormFields(conditions);
    }
  }

	/**
	 * 提交参数的改变
	 * @param params
	 */
  @action
  changeSubmitparams = (params) => {
    this.submitparams = { ...this.submitparams, ...params };
  }

	/**
	 * 撤回准备
	 * TODO
	 */
  goWithDraw = (callback) => {
    const { title, signInputRequired = false, requestid, showMyTodo = false } = this.coreparams;
    const { doneSk = [], myTodo = '' } = this.submitparams;
    let { remark } = this.submitparams;
    let isPass = false,
      showMsgContent = '';
    // 第一步校验选择项
    // 分管理员还是非管理员

    if (doneSk.length > 0 || myTodo == '1') {
      isPass = true;
    } else if (showMyTodo) {
      showMsgContent = getLabel(519331, '请选择要撤回的在办人员或选择恢复我的办理权限！');
    } else {
      showMsgContent = `${getLabel(511633, '请选择要撤回的在办人员')}!`;
    }


    if (!isPass) {
      this.showErrorMsg(showMsgContent, 3);
      return;
    }
    // 第二步校验签字意见
    isPass = false;
    if (signInputRequired) {
      if (window.CK_REF && window.CK_REF[`${this.refid}Edit`]) {
        // remark = window.CK_REF[this.refid + "Edit"].getEditor().getData() || remark;
        remark = window.CK_REF[`${this.refid}Edit`].getEditor().getData();
      }
      if (remark && remark.length > 0) {
        this.submitparams.remark = remark;
        isPass = true;
      } else {
        showMsgContent = getLabel(521596, '请填写$title$意见！');
        showMsgContent = showMsgContent.replace('$title$', title);
      }
    }
    if (!isPass) {
      this.showErrorMsg(showMsgContent, 3);
      return;
    }
    // 第三步用户选择是否确定撤回

    // 第四步：执行撤回请求
    this.doWithDraw({ requestid }, callback);
  }

	/**
	 * 真正的撤回接口
	 * @param params 必须含有流程id【requestid】
	 */
  doWithDraw = (params = {}, callback) => {
    params = { ...params, ...this.submitparams, ...this.workparams };
    API.doWithDraw(params).then((res) => {
      const { api_status = false, status = false, api_message = '' } = res;
      if (api_status || status) {
        this.showSuccessMsg(api_message || getLabel(384408, '撤回成功！'), 3, () => {
          typeof callback == 'function' && (callback(res));
        });
      } else {
        this.showErrorMsg(api_message || getLabel(384409, '撤回失败！'), 3);
      }
    });
  }
	/**
	 * 关闭销毁方法
	 */
  destory = () => {
    this.submitparams = [];
  }
}
